/* >>>>>>>>>>>>>>>>>>>>>>>> 初始化 <<<<<<<<<<<<<<<<<<<<<<<< */
$().ready(function(){
    // 页面初始化
    InitPageInfo();
    // 事件绑定
    InitPageEvent();
});

/* >>>>>>>>>>>>>>>>>>>>>>>> 方法函数 <<<<<<<<<<<<<<<<<<<<<<<< */
/**
 * 页面初始化
 */
function InitPageInfo(){
	InitFormSave();
    InitUpload();
	
	var albumphoto = $("#mainphoto").val();
	if(albumphoto.length>0){
		$(".main_photo").attr("src", albumphoto);
	}
    $("#postdate").datepicker({
        dateFormat: 'yy-mm-dd'
    }).attr("readonly", true);
	
	$("#isshow").val($("#hidden_isshow").val());
}

/**
 * 事件绑定
 */
function InitPageEvent(){
	$(".resetFrom").click(function(){
        $("#videoEditor").resetForm();
        $(".main_photo").attr("src", "/images/manage/blank.gif");
        $('#id').val("");
        return false;
    }).easyconfirm({
        locale: {
            title: '操作确认',
            button: ['取消', '是的，确定重置']
        }
    });
	
    $(".saveBtn").click(function(){
        $("#videoEditor").submit();
        return false;
    });
}

/**
 * 初始化上传
 */
function InitUpload(){
    var images_script = "/action/master/VideoImageUpload";
    var uploader = '../js-plugin/uploadify/uploadify_cn.swf';
    var cancelImg = '../js-plugin/uploadify/cancel.png';
    var folder = '/upload';
    var identifies = {
        'identifies': $("#identifies").val()
    };
    $('#images_file').uploadify({
        'uploader': uploader,
        'script': images_script,
        'cancelImg': cancelImg,
        'scriptData': identifies,//解决firefox 的session bug
        'folder': folder,
        'auto': true,
        'multi': true,
        'fileExt': '*.jpg;*.gif;*.png;*.bmp',
        'fileDesc': '请选择图片文件 (.JPG, .GIF, .PNG, .BMP)',
        'queueID': 'custom-queue',
        'buttonText': "选择上传图片",
		'width':110,
        'removeCompleted': true,
        'onComplete': function(e, queueId, fileObj, response, data){
            var obj = $.parseJSON(response);
            if (obj.success) {
				$(".main_photo").attr("src", obj.filepath);
				$("#mainphoto").val(obj.filepath);
                
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: "succ",
                    loadIcon: "",
                    msgHtml: fileObj.name + " 上传成功"
                });
                $(".loadingbox").fadeIn(50);
                $(".loadingbox").delay(2000).fadeOut(600);
            }
            else {
                $(".loadingbox").empty();
                $(".loadingbox").append(loadingTpl, {
                    icoType: 'fail',
                    loadIcon: '',
                    msgHtml: obj.message
                });
                $(".loadingbox").fadeIn(50);
                $(".loadingbox").delay(3000).fadeOut(600);
            }
        }
    });
}

/**
 * 初始化保存
 */
function InitFormSave(){
    // 中文字两个字节       
    jQuery.validator.addMethod("byteRangeLength", function(value, element, param){
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) || (length >= param[0] && length <= param[1]);
    }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");
    
    $("#videoEditor").validate({
        submitHandler: function(form){
            if ($("#mainphoto").val() =="") {
                $("#errormsg").text("请设定视频封面");
                $(".post-state").removeClass("success").addClass("error").fadeTo("slow", 1).fadeIn(400).delay(4000).slideUp(400);
                return false;
            };
            $(form).ajaxSubmit({
                target: "#errormsg",
                data: {
                },
                dataType: "json",
                beforeSubmit: function(){
                    $(".loadingbox").append(loadingTpl, {
                        icoType: 'clear',
                        loadIcon: '<img src=\"/images/manage/loading002.gif\">',
                        msgHtml: '正在提交数据，请稍候...'
                    });
                    $(".loading").fadeIn(50);
                },
                success: function(data){
                    if (!data.success) {
                        $(".loadingbox").empty();
                        $(".loadingbox").append(loadingTpl, {
                            icoType: 'fail',
                            loadIcon: '',
                            msgHtml: data.message
                        });
                        $(".loadingbox").fadeIn(50);
                        $(".loadingbox").delay(3000).fadeOut(600);
                    }
                    else {
                        $(".loadingbox").empty();
                        $(".loadingbox").append(loadingTpl, {
                            icoType: "succ",
                            loadIcon: "",
                            msgHtml: data.message
                        });
                        $(".loadingbox").fadeIn(50);
                        $(".loadingbox").delay(2000).fadeOut(600);
                    }
                }
            });
        },
        rules: {
            title: {
                required: true,
                byteRangeLength: [4, 200]
            },
            serpath: {
                required: true,
            },
            content: {
                required: true,
                byteRangeLength: [4, 2000]
            }
        },
        messages: {
            title: {
                required: "请填写视频名称",
                byteRangeLength: "图集名称必须在4-200个字符之间(一个中文字算2个字符)"
            },
            serpath: {
                required: "请填写视频地址",
            },
            content: {
                required: "请填写视频说明",
                byteRangeLength: "视频说明必须在4-2000个字符之间(一个中文字算2个字符)"
            }
        }
    });
}
